package com.inspinia.upms.rpc.service;

import com.inspinia.base.common.exception.BusinessException;
import com.inspinia.base.dao.BaseDao;
import com.inspinia.base.pagination.Pager;
import com.inspinia.base.service.BaseServiceImpl;
import com.inspinia.base.util.CollectionUtil;
import com.inspinia.upms.common.model.dept.UpmsDept;
import com.inspinia.upms.common.model.dept.UpmsDeptVo;
import com.inspinia.upms.dao.UpmsDeptDao;
import com.inspinia.upms.rpc.api.UpmsDeptService;
import com.inspinia.upms.rpc.api.UpmsOrganizationService;
import com.inspinia.upms.rpc.api.UpmsUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 部门管理 服务实现
 * 
 * @author veblen
 */
@Service
public class UpmsDeptServiceImpl extends BaseServiceImpl<UpmsDept> implements UpmsDeptService {

    @Resource
    private UpmsDeptDao departmentDao;
    @Resource
    private UpmsUserService upmsUserService;
    @Resource
    private UpmsOrganizationService upmsOrganizationService;

    public static final int MAX_WEIGHTS = 100;

    @Override
    protected BaseDao<UpmsDept> getDao() {
        return departmentDao;
    }

    @Override
    public int delete(Long id) {
        deleteUserByDept(new Long[]{id});
        return super.delete(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchDelete(Long[] ids) {
        deleteUserByDept(ids);
        return departmentDao.batchDelete(ids);
    }

    /**
     * 根据部门id删除部门和用户的中间表数据
     *
     * @param ids 部门id
     * @return
     */
    private int deleteUserByDept(Long[] ids) {
        return departmentDao.deleteUserByDept(ids);
    }

    @Override
    public Pager<UpmsDeptVo> findPage(UpmsDeptVo departmentVo) throws InvocationTargetException, IllegalAccessException {
        Pager page = super.findPage(departmentVo);
        //只获取当前选中机构的部门，子部门不获取
        List<UpmsDeptVo> vos = new ArrayList<>(page.getList().size());
        for (UpmsDept dept : (List<UpmsDept>) page.getList()) {
            UpmsDeptVo vo = new UpmsDeptVo();
            BeanUtils.copyProperties(dept,vo);
            vo.setOrgName(upmsOrganizationService.getNameById(dept.getOrgId()));
            vos.add(vo);
        }
        page.setList(vos);
        return page;
    }

    @Override
    public int insert(UpmsDept bean) {

        validateDept(bean);

        int i = super.insert(bean);
        return i;

    }

    /**
     * 对部门进行校验
     *
     * @param bean
     */
    private void validateDept(UpmsDept bean) {
        boolean b = validateDeptName(bean.getDeptName(), bean.getOrgId(), bean.getId());
        if (b) {
            throw new BusinessException("名称已存在");
        }
    }

    /**
     * 验证部门名称
     *
     * @param name
     * @param orgId
     * @param id
     * @return
     */
    private boolean validateDeptName(String name, Long orgId, Long id) {
        return departmentDao.countExistedByName(name, orgId, id) > 0;
    }

    @Override
    public int update(UpmsDept bean) {
//        setDefaultWeights(bean);
        validateDept(bean);
        return super.update(bean);
    }

    @Override
    public List<Long> findDeptIdsByOrgIds(Collection<Long> orgIds) {
        return departmentDao.findDeptIdsByOrgIds(orgIds);
    }

    @Override
    public List<UpmsDept> findDeptByUserOrg(Long userId) {
        return departmentDao.findDeptByUserOrg(userId);
    }

    @Override
    public List<UpmsDept> findAll() {
        return departmentDao.findAll();
    }

    @Override
    public List<Long> findDeptIdsByUserId(Long userId) {
        return departmentDao.findDeptIdsByUserId(userId);
    }

    @Override
    public List<String> findNamesByUserId(Long userId) {
        List<Long> deptIds = findDeptIdsByUserId(userId);
        if (CollectionUtil.isNotEmpty(deptIds)) {
            return departmentDao.findNamesByIds(deptIds);
        }
        return Collections.emptyList();
    }

    @Override
    public UpmsDeptVo getVo(long id) throws InvocationTargetException, IllegalAccessException {
        UpmsDept department = get(id);
        UpmsDeptVo vo = new UpmsDeptVo();
        BeanUtils.copyProperties(department,vo);
        vo.setOrgName(upmsOrganizationService.getNameById(vo.getOrgId()));
        vo.setLeaderName(upmsUserService.getRealNameById(vo.getLeader()));
        return vo;
    }

    @Override
    public Long getOrgId(Long deptId) {
        return departmentDao.getOrgIdByDeptId(deptId);
    }

    @Override
    public String getName(Long deptId) {
        return departmentDao.getNameById(deptId);
    }

    @Override
    public List<UpmsDept> findByUserId(Long userId) {
        List<Long> ids = findDeptIdsByUserId(userId);
        if (CollectionUtil.isNotEmpty(ids)) {
            return departmentDao.findByIds(ids);
        }
        return null;
    }

    @Override
    public List<Long> findOrgIds(List<Long> deptIds) {
        if (CollectionUtil.isNotEmpty(deptIds)) {
            return departmentDao.findOrgIds(deptIds);
        }
        return null;
    }

    @Override
    public List<Long> findDeptIds(Long cityId, String deptName) {
        return departmentDao.findDeptIds(cityId, deptName);
    }

    @Override
    public String findPhone(Long userId) {
        List<UpmsDept> departmentList = findByUserId(userId);
        if (departmentList != null && departmentList.size() > 0) {
            return null;
        }
        return null;
    }

    @Override
    public List<Long> findUserIdsByDeptId(List<Long> deptIds) {
        return departmentDao.findUserIdsByDeptId(deptIds);
    }

    @Override
    public String getOfficialSealUrlByDeptId(Long deptId) {
        return departmentDao.getOfficialSealUrlByDeptId(deptId);
    }

    @Override
    public List<UpmsDept> findByOrgId(Long orgId) {
        return departmentDao.findByOrgId(orgId);
    }

    @Override
    public List<UpmsDept> findDeptByUserOrgExcludeOneSelf(Long userId) {
        return departmentDao.findDeptByUserOrgExcludeOneSelf(userId);
    }

    @Override
    public List<UpmsDept> findDeptByUserOrgExcludeOneSelfAndDept(Long userId, Boolean noOneSelf, Long createDeptId) {
        return departmentDao.findDeptByUserOrgExcludeOneSelfAndDept(userId, noOneSelf, createDeptId);
    }

    @Override
    public UpmsDept findCommandCenterNameByOrgId(Long orgId) {
        return departmentDao.findCommandCenterNameByOrgId(orgId);
    }

    @Override
    public List<Long> findDeptIdsInOrg(Long orgId, String deptName) {
        return departmentDao.findDeptIdsInOrg(orgId, deptName);
    }
}
